package com.zenchn.service.impl;

import com.zenchn.dao.TMiCustomerVisitMapper;
import com.zenchn.dynamic.TMiCustomerVisitDynamicMapper;
import com.zenchn.entity.VisitCountBO;
import com.zenchn.generic.GenericDao;
import com.zenchn.generic.GenericServiceImpl;
import com.zenchn.model.TMiCustomerVisit;
import com.zenchn.model.TMiCustomerVisitExample;
import com.zenchn.service.CustomerVisitService;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static com.zenchn.dynamic.TMiCustomerDynamicSqlSupport.TMiCustomer;
import static com.zenchn.dynamic.TMiCustomerVisitDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

/**
 * @author wlf
 * @version 1.0
 * @date 2021/6/7 13:13:27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerVisitServiceImpl extends GenericServiceImpl<TMiCustomerVisit, String, TMiCustomerVisitExample> implements CustomerVisitService {

    @Resource
    private TMiCustomerVisitMapper mapper;

    @Resource
    private TMiCustomerVisitDynamicMapper dynamicMapper;

    @Override
    public GenericDao<TMiCustomerVisit, String, TMiCustomerVisitExample> getDao() {
        return mapper;
    }

    @Override
    public List<TMiCustomerVisit> selectDynamicSqlByParameter(String keyword, List<String> areasCodeList, String areasCode, String accountId, String operatorId, String customerType, LocalDate startDate, LocalDate endDate) {
        QueryExpressionDSL<SelectModel> dsl = select(TMiCustomerVisit.allColumns()).from(TMiCustomerVisit);
        dsl.leftJoin(TMiCustomer).on(TMiCustomer.customerId, equalTo(TMiCustomerVisit.customerId));

        QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder dslWhere = dsl.where();
        if (null != areasCodeList && !areasCodeList.isEmpty()) {
            dslWhere.and(TMiCustomer.areasCode, isIn(areasCodeList));
        }

        if (null != areasCode && !"".equals(areasCode.trim())) {
            dslWhere.and(TMiCustomer.areasCode, isEqualTo(areasCode));
        }

        if (null != accountId && !"".equals(accountId.trim())) {
            dslWhere.and(TMiCustomer.accountId, isEqualTo(accountId));
        }

        if (null != operatorId && !"".equals(operatorId.trim())) {
            dslWhere.and(TMiCustomerVisit.creator, isEqualTo(operatorId));
        }

        if (null != customerType && !"".equals(customerType.trim())) {
            dslWhere.and(TMiCustomer.customerType, isEqualTo(customerType));
        }

        if (null != startDate) {
            dslWhere.and(TMiCustomerVisit.visitDate, isGreaterThanOrEqualTo(startDate));
        }

        if (null != endDate) {
            dslWhere.and(TMiCustomerVisit.visitDate, isLessThanOrEqualTo(endDate));
        }

        if (null != keyword && !"".equals(keyword.trim())) {
            keyword = "%" + keyword.trim() + "%";
            dslWhere.and(TMiCustomer.customerName, isLike(keyword));
        }
        SelectStatementProvider selectStatement = dsl.groupBy(TMiCustomerVisit.visitId).orderBy(TMiCustomerVisit.visitDate.descending()).build().render(RenderingStrategies.MYBATIS3);

        return dynamicMapper.selectMany(selectStatement);
    }

    @Override
    public VisitCountBO getCustomerVisit(LocalDate startDate, LocalDate endDate, String accountId) {
        return mapper.getCustomerVisit(startDate,endDate,accountId);
    }
}
